package com.yiqi.news.mvp.ui.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import butterknife.OnClick

import com.jess.arms.base.BaseActivity
import com.jess.arms.di.component.AppComponent
import com.jess.arms.utils.ArmsUtils
import com.yiqi.huize.mvp.dialog.OpenPageDialog

import com.yiqi.news.di.component.DaggerSettingComponent
import com.yiqi.news.di.module.SettingModule
import com.yiqi.news.mvp.contract.SettingContract
import com.yiqi.news.mvp.presenter.SettingPresenter

import com.yiqi.news.R
import com.yiqi.news.app.service.UserController
import com.yiqi.news.app.utils.CacheManager
import com.yiqi.news.app.utils.SPUtil
import com.yiqi.news.app.utils.SPUtil.SP_NAME
import com.yiqi.news.app.widget.qmui.QMUITipDialog
import kotlinx.android.synthetic.main.activity_setting.*


class SettingActivity : BaseActivity<SettingPresenter>(), SettingContract.View, OpenPageDialog.OnClickListener {
    override fun onOpen() {
        UserController.getInstance().loginOut()
        SPUtil.clear(this, SP_NAME)
        killMyself()
        ArmsUtils.startActivity(LoginActivity::class.java)
    }

    override fun setupActivityComponent(appComponent: AppComponent) {
        DaggerSettingComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .settingModule(SettingModule(this))
                .build()
                .inject(this)
    }

    private val mOpenPageDialog by lazy {
        return@lazy OpenPageDialog(this)
    }

    override fun initView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_setting //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }


    override fun initData(savedInstanceState: Bundle?) {
        tv_setting_cache.text = CacheManager.getTotalCacheSize(this)
    }


    override fun showMessage(message: String) {
        ArmsUtils.snackbarText(message)
    }

    override fun launchActivity(intent: Intent) {
        ArmsUtils.startActivity(intent)
    }

    override fun killMyself() {
        finish()
    }

    @OnClick(R.id.btn_setting_logout, R.id.tv_setting_cache,R.id.btn_setting_agreement)
    fun onClick(view: View) {
        when (view.id) {
            R.id.tv_setting_cache -> {
                CacheManager.clearAllCache(this)
                mQMUIInfoDialog.show()
                tv_setting_cache.text = CacheManager.getTotalCacheSize(this)
            }
            R.id.btn_setting_logout -> {
                mOpenPageDialog.setDesc("确认退出？")
                mOpenPageDialog.show()
                mOpenPageDialog.setOnClickListener(this)
            }
            R.id.btn_setting_agreement->{
                var bundle=Bundle()
                bundle.putBoolean("is_agg",true)
                ArmsUtils.startActivity(this,AgreementActivity::class.java,bundle)
            }
        }
    }

    private val mQMUIInfoDialog: QMUITipDialog by lazy {
        return@lazy QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_SUCCESS)
                .setTipWord("清除缓存成功")
                .create()
    }

    override fun initImmersionBar() {
        super.initImmersionBar()
        mImmersionBar.titleBar(R.id.toolbar).init()
    }
}
